'use strict';

angular.module('app')
    .controller('activityDetailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "活动详情";
            var id = $state.params.id;
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            activate(id);
            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/activity/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $('#preStartTime').val($scope.record.preStartTime);
    		            $('#preEndTime').val($scope.record.preEndTime);
    		            $('#activityStartTime').val($scope.record.activityStartTime);
    		            $('#activityEndTime').val($scope.record.activityEndTime);
    		            $scope.imgData = [];
    		            var srcArr = [];
    		            srcArr.push($scope.record.picUrl);
    		            $scope.imgData.push({
    	                    src:srcArr
    	                })
    	                
    	                $scope.detailImgData = [];
			            var detailArr = [];
			            detailArr.push($scope.record.detailUrl);
			            $scope.detailImgData.push({
		                    src:detailArr
		                })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            /*$scope.saveActivity = function(id) {
            //$scope.submit = function(){
                $scope.loading = true;
                
                if(!$('#theme').val()){
                    $scope.alertModal({'content':'活动名称不能为空'});
                    return;
                }
                
                if(!$('#city').val()){
                    $scope.alertModal({'content':'主办地不能为空'});
                    return;
                }
                
                if(!$('#hostAddr').val()){
                    $scope.alertModal({'content':'活动主办地址不能为空'});
                    return;
                }
                
                if(!$('#preStartTime').val()){
                    $scope.alertModal({'content':'报名开始日期不能为空'});
                    return;
                }
                
                if(!$('#preEndTime').val()){
                    $scope.alertModal({'content':'报名结束日期不能为空'});
                    return;
                }
                
                if (!checkEndTime($('#preStartTime').val(), $('#preEndTime').val())) {
                	$scope.alertModal({'content':'报名开始日期不能大于报名结束日期'});
            		return;
            	}
                
                if(!$('#activityStartTime').val()){
                    $scope.alertModal({'content':'活动开始日期不能为空'});
                    return;
                }
                
                if(!$('#activityEndTime').val()){
                    $scope.alertModal({'content':'活动结束日期不能为空'});
                    return;
                }
                
                if (!checkEndTime($('#activityStartTime').val(), $('#activityEndTime').val())) {
                	$scope.alertModal({'content':'活动开始时间不能大于活动结束时间'});
            		return;
            	}
                
                var preStartTime = $('#preStartTime').val() + " 00:00:00";
                var preEndTime = $('#preEndTime').val() + " 23:59:59";
                $scope.record.preStartTime = preStartTime;
                $scope.record.preEndTime = preEndTime;
                
                if (!checkEndTime(preStartTime, $('#activityStartTime').val())) {
                	$scope.alertModal({'content':'报名日期不能大于活动开始时间'});
            		return;
            	}
                
                if (!checkEndTime(preEndTime, $('#activityStartTime').val())) {
                	$scope.alertModal({'content':'报名日期不能大于活动开始时间'});
            		return;
            	}
                
                if (!checkEndTime(preEndTime, $('#activityEndTime').val())) {
                	$scope.alertModal({'content':'报名结束日期不能大于活动结束时间'});
            		return;
            	}
                
                if(!$('#detailUrl').val()){
                    $scope.alertModal({'content':'活动详情不能为空'});
                    return;
                }
                
                if(!$('#organizer').val()){
                    $scope.alertModal({'content':'主办单位不能为空'});
                    return;
                }
                
                if(!$('#contractor').val()){
                    $scope.alertModal({'content':'承办单位不能为空'});
                    return;
                }
                
                $scope.record.activityStartTime = $('#activityStartTime').val();
                $scope.record.activityEndTime = $('#activityEndTime').val();
                
                var picUrl = $scope.imgData[0].src;
                if (!picUrl) {
                	$scope.alertModal({'content':'请上传活动图片'});
                    return;
                }

                $scope.record.picUrl = picUrl[0];
                
                var m = $scope.record;
                $scope.isDisabled = true;//提交disabled
                
                if (m) {
                    $.ajax({
        				url : $scope.record.id ? $scope.app.project_name + '/activity/update' : $scope.app.project_name + '/activity/add',
        				data: $scope.record
        			}).then(callback);
                }
                function callback(result) {
                    if(result.httpCode ==200){//成功
                        $timeout(function(){
                        	$scope.isDisabled = false;
                            $state.go('main.funScenes.activity.list');
                        },2000);
                    }else{
                    	$scope.isDisabled = false;
                    	$scope.alertModal({"content":"保存失败"});
                    }
                }
            };*/
            
            function checkEndTime(startTime, endTime) {  
        	    var start = new Date(startTime.replace("-", "/").replace("-", "/"));  
        	    var end = new Date(endTime.replace("-", "/").replace("-", "/"));  
        	    if (end < start) {  
        	        return false;  
        	    }  
        	    return true;  
        	}
            
            $scope.delActivity = function(id) {
            	$scope.alertMessage({
                    text:"确定要删除该活动吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/activity/update/delActivity',
        				data: {'id': id}
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$state.go('main.funScenes.activity.list');
        					//$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }

    } ]);